﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MKDiractorySearch;
using MovieModels;
using MKDataBase.Contracts;

namespace MK_Movie_Manager
{
    public partial class SearchAndUpdate : UserControl
    {
        private IDBProvider iDBProvider;
        private MKMovieManager_MainForm MainForm;

        public SearchAndUpdate(IDBProvider iDBProvider, MKMovieManager_MainForm mKMovieManager_MainForm)
        {
            InitializeComponent();
            this.iDBProvider = iDBProvider;
            this.MainForm = mKMovieManager_MainForm;
        }
        public List<SearchedMovieFile> files { get; set; }
        public IEnumerable<MKFile> movieFiles { get; set; }
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            DialogResult res = this.searchFolderBrowse.ShowDialog();
            if (res.Equals(DialogResult.OK))
            {
                this.txtFolderPath.Text = this.searchFolderBrowse.SelectedPath;
            }
        }

        private void btnSearchDir_Click(object sender, EventArgs e)
        {
            var folderpath = this.txtFolderPath.Text.Trim();
            if (!string.IsNullOrWhiteSpace(folderpath))
            {
                //BrowseForMovies
                this.panelSearchResult.Controls.Clear();
                MKDirectorySearch dirSearch = new MKDirectorySearch(folderpath);
                UpdateSearchResultpanel(dirSearch);
            }
        }

        private void UpdateSearchResultpanel(MKDirectorySearch dirSearch)
        {
            files = new List<SearchedMovieFile>();
            movieFiles = FilterMovieFiles(dirSearch.FileSysNames);
            for (int i = 0; i < movieFiles.Count(); i++)
            {
                var movie = movieFiles.ElementAt(i);
                var searchedMovie = new SearchedMovieFile(movie);
                files.Add(searchedMovie);

                searchedMovie.Location = new System.Drawing.Point(4, 4 + i + (19 * (i + 1)));
                searchedMovie.Size = new System.Drawing.Size(600, 19);
                searchedMovie.TabIndex = i;
                this.panelSearchResult.Controls.Add(searchedMovie);
            }

        }

        private IEnumerable<MKFile> FilterMovieFiles(IEnumerable<MKFile> movieFiles)
        {
            var supportedTypes = Properties.Settings.Default.MovieTypesSupported;
            movieFiles = movieFiles.Where(m => m.Type.Equals(FileType.Video)).
                Where(m => supportedTypes.Contains(m.Extension.Trim(new char[] { '.', ' ' }))).
                Where(m => m.Size >= (200 * 1024 * 1024));
            return movieFiles;
        }

        private void btn_AddAll_Click(object sender, EventArgs e)
        {
            if (files.Count <= 0)
                return;
            for(int i = 0; i<files.Count;i++)
            {
                if (!files[i].IsChecked)
                    continue;
                DBStatus status = this.iDBProvider.AddAMovie(new MovieManagerDataRow
                {
                    Language = "Not Set",
                    MovieName = files[i].FileName,
                    MoviePath = files[i].Path
                });
            }
            MessageBox.Show("Successfully added.");
            this.MainForm.ReadMoviesFromDB();
        }
    }
}
